package com.xy.xyaicpzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.xyaicpzs.domain.entity.DltPredictRecord;
import com.xy.xyaicpzs.service.DltPredictRecordService;
import com.xy.xyaicpzs.mapper.DltPredictRecordMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author XY003
* @description 针对表【dlt_predict_record(大乐透推测记录表)】的数据库操作Service实现
* @createDate 2025-09-08 14:01:12
*/
@Service
public class DltPredictRecordServiceImpl extends ServiceImpl<DltPredictRecordMapper, DltPredictRecord>
    implements DltPredictRecordService{

    @Override
    public DltPredictRecord createDltPredictRecord(Long userId, Long drawId, Date drawDate, List<Integer> frontBalls, List<Integer> backBalls) {
        DltPredictRecord predictRecord = new DltPredictRecord();
        predictRecord.setUserId(userId);
        predictRecord.setDrawId(drawId);
        predictRecord.setDrawDate(drawDate);
        predictRecord.setPredictTime(new Date());
        
        // 设置前区球号码（5个）
        if (frontBalls != null && frontBalls.size() >= 5) {
            predictRecord.setFrontendBall1(frontBalls.get(0));
            predictRecord.setFrontendBall2(frontBalls.get(1));
            predictRecord.setFrontendBall3(frontBalls.get(2));
            predictRecord.setFrontendBall4(frontBalls.get(3));
            predictRecord.setFrontendBall5(frontBalls.get(4));
        }
        
        // 设置后区球号码（2个）
        if (backBalls != null && backBalls.size() >= 2) {
            predictRecord.setBackendBall1(backBalls.get(0));
            predictRecord.setBackendBall2(backBalls.get(1));
        }
        
        // 默认状态为待开奖
        predictRecord.setPredictStatus("待开奖");
        
        save(predictRecord);
        return predictRecord;
    }

    @Override
    public List<DltPredictRecord> getDltPredictRecordsByUserIdWithPaging(Long userId, Integer page, Integer size) {
        QueryWrapper<DltPredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId).orderByDesc("predictTime");
        
        // 计算偏移量
        int offset = (page - 1) * size;
        queryWrapper.last("LIMIT " + offset + ", " + size);
        
        return list(queryWrapper);
    }

    @Override
    public Long getDltPredictRecordsCountByUserId(Long userId) {
        QueryWrapper<DltPredictRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        return count(queryWrapper);
    }

}




